﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Help_Us.DataSet.DataSetOrgTableAdapters;
using Help_Us.DataSet;

namespace Help_Us.DataAccess
{
    public class OrganizacaoControle
    {
        private enderecoSet_usuOrganizacaoTableAdapter _organizacaoTableAdapter;

        private DataSetOrg.enderecoSet_usuOrganizacaoDataTable data;

        public enderecoSet_usuOrganizacaoTableAdapter OrganizacaoTableAdapter
        {
             get { 
                 if(_organizacaoTableAdapter == null){              
                    _organizacaoTableAdapter = new enderecoSet_usuOrganizacaoTableAdapter(); 
                 }
                 return _organizacaoTableAdapter;
            }
        }
        public DataSetOrg.enderecoSet_usuOrganizacaoDataTable consultaOrganizacao()
        {
            return OrganizacaoTableAdapter.GetData();
        }


        public DataSetOrg.enderecoSet_usuOrganizacaoDataTable consultaOrganizacaoByIdOrg(int idOrganizacao)
        {
            //idOrganizacao = 1111;
            data = new DataSetOrg.enderecoSet_usuOrganizacaoDataTable();
            try
            {
                data = OrganizacaoTableAdapter.GetDataByIdOrg(idOrganizacao);
                return data;
            }
            catch (Exception ex)
            {
                ex.GetType();
                return null;
            }
        }
        public int alteraOrganizacaoByIdOrg(Int32 idOrganizacao, Int32 cnpjOrg, Int32 inscEstOrg, String nomeOrg, String telOrg)
        {
            //idOrganizacao = 1111;
            data = new DataSetOrg.enderecoSet_usuOrganizacaoDataTable();
            int executou = OrganizacaoTableAdapter.UpdateQueryByIdOrg(cnpjOrg, nomeOrg, telOrg, inscEstOrg, idOrganizacao);
            return executou;
        }

        public DataSetOrg.enderecoSet_usuOrganizacaoDataTable realizarLogin(String usuario, String senha)
        {
            
            data = new DataSetOrg.enderecoSet_usuOrganizacaoDataTable();
            try
            {
                data = OrganizacaoTableAdapter.GetLogin(usuario, senha);
                return data;
            }
            catch (Exception ex)
            {
                ex.GetType();
                return null;
            }
        }
        public int insereOrganizacao(Int32 cnpjOrg, string nomeOrg, string emailUsuRes, string nomeUsuRe, string senhaUsuRes, string telOrg, Int32 inscEstOrg)
        {
            int idOrganizacaoLocal = 0;
            //idOrganizacao = 1111;
            nomeUsuRe = "11111";
            emailUsuRes = "11111";
            senhaUsuRes = "11111";

            data = new DataSetOrg.enderecoSet_usuOrganizacaoDataTable();
            int executou = 0;
            executou = OrganizacaoTableAdapter.InsertOrg(cnpjOrg, nomeOrg, emailUsuRes, nomeUsuRe, senhaUsuRes, telOrg, inscEstOrg);
            if (executou > 0)
            {
                DataSetOrg.enderecoSet_usuOrganizacaoDataTable organizacaoDataTable = OrganizacaoTableAdapter.GetDataByCNPJ(cnpjOrg);
                if (organizacaoDataTable != null)
                {
                    foreach (DataSetOrg.enderecoSet_usuOrganizacaoRow row in organizacaoDataTable.Rows)
                    {
                        idOrganizacaoLocal = row.idOrg;
                        return idOrganizacaoLocal;

                    }
                }
            }
            return executou;
        }
    }
}